package com.mc.test;

import java.util.Stack;

/**
 * @Description: 给定一组括号，判断这组括号能否完成匹配
 * @Author: MoChen
 */
public class Brancket {

    static String bracketStr = "({}{})";

    public static void main(String[] args) {
        System.out.println(bracketCheck(bracketStr));
    }

    static boolean bracketCheck(String bracketStr){
        char[] data = new char[bracketStr.length()]; // 栈元素
        int top = 0; // 栈顶指针
        char[] strs = bracketStr.toCharArray();
        for(int i = 0; i < strs.length; i++){
            if(strs[i] == '[' || strs[i] == '{' || strs[i] == '('){ // 扫描到左括号，入栈
                data[++top] = strs[i];
            }else{
                if(top == 0){ // 扫描到右括号且当前栈空,匹配失败
                    return false;
                }
                char topElem = data[top--]; // 栈顶元素出栈
                // 进行匹配，如果匹配失败则返回false
                if(strs[i] == ')' && topElem != '('){
                    return false;
                }
                if(strs[i] == ']' && topElem != '['){
                    return false;
                }
                if(strs[i] == '}' && topElem != '{'){
                    return false;
                }
            }
        }
        return top == 0; // 括号校验完成且栈空则匹配成功
    }
}
